package com.softer.chengxin.project.dao;

import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.project.dao.provider.ProjectDaoProvider;
import com.softer.chengxin.project.domin.Project;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2018/9/26
 */
@Mapper
@Repository
public interface ProjectDao {
    /**
     * 查询所有项目列表
     * @param id 企业ID
     * @return 项目列表
     */
    @Select("SELECT id,project_name as projectName FROM company_project WHERE company_id=#{id}")
    List<Project> getAllProject(@Param("id") Integer id);

    /**
     * 查询企业项目列表
     *
     * @param id        企业ID
     * @param projectId 项目ID(当为0时，查询企业下的所有项目，否则查询该项目信息)
     * @return 项目列表
     */
    @Select("SELECT id,project_name as projectName FROM company_project WHERE IF(#{projectId}!=0,id=#{projectId},1=1) AND company_id=#{id} AND state=2 ")
    List<Project> queryProjectList(@Param("id") Integer id, @Param("projectId") String projectId);

    /**
     * 删除项目
     *
     * @param id 项目ID
     * @return 结果
     */
    @Delete("DELETE FROM company_project WHERE id =#{id}")
    int delete(Integer id);

    /**
     * 提交项目
     *
     * @param id 项目ID
     * @return 结果
     */
    @Update("UPDATE company_project SET state=1 WHERE id=#{id}")
    int submit(Integer id);

    /**
     * 查询项目信息数量
     *
     * @param project 查询实体
     * @param states  不同的页面查询不同的状态信息
     * @return 数量
     */
    @SelectProvider(type = ProjectDaoProvider.class, method = "queryNum")
    int queryNum(@Param("project") Project project, @Param("states") String states);

    /**
     * 查询项目信息列表
     *
     * @param page    页面实体
     * @param project 查询实体
     * @param states  不同的页面查询不同的状态信息
     * @return 列表
     */
    @SelectProvider(type = ProjectDaoProvider.class, method = "queryList")
    List<Project> queryList(@Param("page") Page page, @Param("project") Project project, @Param("states") String states);

    /**
     * 增加项目信息
     *
     * @param project 项目实体
     * @return 增加结果
     */
    @Insert("INSERT INTO company_project(gmt_create,gmt_modified,project_id,project_name,company_id,property_type,province,city,district,street,layout_plan,service_contract,founding_time," +
            "project_address,project_develop,with_elevator_area,public_facilities_area,housing_quantity,non_residential_quantity,contract_path,is_intelligent,detailed_address,is_committee," +
            "number_residents,elevators,committee_time,committee_director,east_end,west_end,south_end,north_end,project_area,remark,air_scape,entrust_begin_time,term," +
            "management_phone,building_land,green_area,municipal,other_area,all_area,get_type,takeover_time,state,is_old,is_indemnificatory,finish_time,entrust_end_time)" +
            " VALUES(NOW(),NOW(),#{p.projectId},#{p.projectName},#{p.companyId},#{p.propertyType},#{p.province},#{p.city},#{p.district},#{p.street},#{p.layoutPlan},#{p.serviceContract},#{p.foundingTime}," +
            "#{p.projectAddress},#{p.projectDevelop},#{p.withElevatorArea},#{p.publicFacilitiesArea},#{p.housingQuantity},#{p.nonResidentialQuantity},#{p.contractPath},#{p.isIntelligent},#{p.detailedAddress},#{p.isCommittee}," +
            "#{p.numberResidents},#{p.elevators},#{p.committeeDate},#{p.committeeDirector},#{p.eastEnd},#{p.westEnd},#{p.southEnd},#{p.northEnd},#{p.projectArea},#{p.remark},#{p.airScape},#{p.entrustBeginTime},#{p.term}," +
            "#{p.managementPhone},#{p.buildingLand},#{p.greenArea},#{p.municipal},#{p.otherArea},#{p.allArea},#{p.getType},#{p.takeoverDate},0,#{p.isOld},#{p.isIndemnificatory},#{p.finishDate},#{p.entrustEndTime})")
    @Options(useGeneratedKeys = true,keyProperty = "p.id",keyColumn = "id")
    int add(@Param("p") Project project);

    /**
     * 查询具体项目信息
     *
     * @param id 项目ID
     * @return 项目信息
     */
    @Select("SELECT p.id,p.project_name as projectName,b.`name` as companyName, p.property_type AS propertyTypeCode,(SELECT GROUP_CONCAT(name) FROM dic_info WHERE FIND_IN_SET(CODE,p.property_type)) AS propertyType,p.province,p.city,p.district,p.street,p.community,p.project_address as projectAddress,p.project_develop as projectDevelop,p.layout_plan as layoutPlan," +
            "p.with_elevator_area as withElevatorArea,p.public_facilities_area as publicFacilitiesArea,p.housing_quantity as housingQuantity,p.non_residential_quantity as nonResidentialQuantity,p.detailed_address as detailedAddress,p.term,p.founding_time as foundingTime," +
            "p.number_residents as numberResidents,p.elevators,committee_time as committeeTime,p.committee_director as committeeDirector,p.east_end as eastEnd,p.west_end as westEnd,p.south_end as southEnd,DATE_FORMAT(entrust_begin_time, '%Y-%m-%d') AS `entrustBeginTime`," +
            "p.north_end as northEnd,p.project_area as projectArea,p.management_phone as managementPhone,p.building_land as buildingLand,green_area as greenArea,p.municipal,p.other_area as otherArea,p.service_contract as serviceContract,is_committee as isCommittee, " +
            "p.all_area as allArea,p.get_type as getType,p.takeover_time as takeoverTime,p.state,is_old as isOld,is_indemnificatory as isIndemnificatory,finish_time as finishTime,p.air_scape as airScape,DATE_FORMAT(entrust_end_time, '%Y-%m-%d') AS `entrustEndTime`," +
            "p.project_id as projectId,is_intelligent as isIntelligent,contract_Path as contractPath,remark FROM company_project p left join company_base_info b ON p.company_id=b.id WHERE p.id=#{id}")
    Project detail(@Param("id") Integer id);

    /**
     * 查询项目审核不通过的审核意见
     *
     * @param id 项目ID
     * @return 审核意见
     */
    @Select("SELECT opinion FROM company_project_opinion WHERE project_id=#{id} ORDER BY gmt_create DESC limit 1")
    String queryProjectOpinion(@Param("id") Integer id);

    /**
     * 编辑项目信息
     *
     * @param project 项目实体
     * @return 编辑结果
     */
    @Update("UPDATE company_project SET gmt_modified=now(),project_name=#{p.projectName},property_type=#{p.propertyType},province=#{p.province},city=#{p.city},district=#{p.district},street=#{p.street},community=#{p.community},project_address =#{p.projectAddress},air_scape =#{p.airScape},entrust_begin_time=#{p.entrustBeginTime}," +
            "with_elevator_area =#{p.withElevatorArea},public_facilities_area =#{p.publicFacilitiesArea},housing_quantity =#{p.housingQuantity},non_residential_quantity =#{p.nonResidentialQuantity},all_area =#{p.allArea},service_contract =#{p.serviceContract},entrust_end_time=#{p.entrustEndTime},is_committee=#{p.isCommittee}, " +
            "number_residents =#{p.numberResidents},elevators=#{p.elevators},committee_time =#{p.committeeDate},committee_director =#{p.committeeDirector},east_end =#{p.eastEnd},west_end =#{p.westEnd},south_end =#{p.southEnd},project_develop =#{p.projectDevelop},detailed_address=#{p.detailedAddress}," +
            "north_end =#{p.northEnd},project_area =#{p.projectArea},management_phone =#{p.managementPhone},building_land =#{p.buildingLand},green_area =#{p.greenArea},municipal=#{p.municipal},other_area =#{p.otherArea},get_type =#{p.getType},layout_plan =#{p.layoutPlan},founding_time=#{p.foundingTime}," +
            "takeover_time =#{p.takeoverDate},state=0,is_old =#{p.isOld},is_indemnificatory =#{p.isIndemnificatory},finish_time =#{p.finishDate},is_intelligent =#{p.isIntelligent},contract_path =#{p.contractPath},remark=#{p.remark},term=#{p.term} WHERE id=#{p.id}")
    int edit(@Param("p") Project project);

    /**
     * 判断项目是否已经创建
     *
     * @param project 项目信息
     * @return 项目名称
     */
    @Select("SELECT project_name FROM company_project WHERE company_id=#{p.companyId} AND project_name=#{p.projectName}")
    String checkProjectName(@Param("p") Project project);

    /**
     * 根据前端创建的projectId查询项目的ID
     *
     * @param projectId 前端创建的projectId
     * @return ID
     */
    @Select("select id from company_project where project_id=#{id}")
    Integer getProjectId(@Param("id") String projectId);

}
